package cn.iocoder.mall.admin.application.controller.system;

import cn.iocoder.common.framework.constant.CommonConstant;
import cn.iocoder.common.utils.common.api.vo.Result;
import cn.iocoder.common.utils.common.util.ObjectUtils;
import cn.iocoder.mall.admin.api.dto.params.SysAppParams;
import cn.iocoder.mall.admin.api.dto.params.SysFianceParams;
import cn.iocoder.mall.admin.api.dto.params.SysSMSParams;
import cn.iocoder.mall.admin.api.dto.params.SystemParamsDTO;
import cn.iocoder.mall.admin.api.util.SystemParamsConvertUtil;
import cn.iocoder.mall.admin.dataobject.SystemParams;
import cn.iocoder.mall.admin.entity.SysParams;
import cn.iocoder.mall.admin.service.ISysParamsService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/***
 *@author yinxucun
 *@date 2019-10-30 16:31 
 */
@RestController
@RequestMapping("/sys/params")
public class SystemParamsController {

    @Autowired
    private ISysParamsService sysParamsService;

    @GetMapping("/getAppVersion")
    public Result getAppVersion() {
        QueryWrapper<SysParams> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", CommonConstant.APP_VERSION_PARAMS_TYPE);
        List<SysParams> list = sysParamsService.list(queryWrapper);
        List<SystemParamsDTO> list1= ObjectUtils.copyList(list,SystemParamsDTO.class);

        return Result.ok(SystemParamsConvertUtil.getAppParams(list1));
    }


    @GetMapping("/getFinancialParameters")
    public Result getFinancialParameters() {
        QueryWrapper<SysParams> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", CommonConstant.FINANCIAL_PARAMETERS_TYPE);
        List<SysParams> list = sysParamsService.list(queryWrapper);
        List<SystemParamsDTO> list1= ObjectUtils.copyList(list,SystemParamsDTO.class);

        return Result.ok(SystemParamsConvertUtil.getSysFiancePrams(list1));
    }

    @GetMapping("/getMessageParams")
    public Result getMessageParams() {
        QueryWrapper<SysParams> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", CommonConstant.MESSAGE_PARAMS_TYPE);
        List<SysParams> list = sysParamsService.list(queryWrapper);
        List<SystemParamsDTO> list1= ObjectUtils.copyList(list,SystemParamsDTO.class);

        return Result.ok(SystemParamsConvertUtil.getSysSMSPrams(list1));
    }

    @PostMapping("/setFinancialParameters")
    public Result<?> setFinancialParameters(@RequestBody SysFianceParams fianceParams) {
        List<SystemParamsDTO> list=SystemParamsConvertUtil.convertToSysParams(fianceParams);
        List<SysParams> params=ObjectUtils.copyList(list,SysParams.class);
        sysParamsService.updateBatchByName(params);
//        sysParamsService.updateBatchByName(params);
        return Result.ok();
    }

    @PostMapping("/setMessageParams")
    public Result<?> setMessageParams(@RequestBody SysSMSParams sysSMSParams) {
        List<SystemParamsDTO> list=SystemParamsConvertUtil.convertToSysParams(sysSMSParams);

        List<SysParams> db=ObjectUtils.copyList(list,SysParams.class);
        sysParamsService.updateBatchByName(db);
        return Result.ok();
    }

    @PostMapping("/setAppVersion")
    public Result<?> setAppVersion(@RequestBody SysAppParams appParams) {

        List<SystemParamsDTO> list=SystemParamsConvertUtil.convertToSysParams(appParams);

        List<SysParams> params=ObjectUtils.copyList(list,SysParams.class);
        sysParamsService.updateBatchByName(params);
        return Result.ok();
    }


}
